﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	WebEntity.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.SharePoint;
using Smart.Common.Security;

namespace Smart.Common.Entities
{
	[Serializable]
	[Entity("WebEntity")]
	[DebuggerDisplay("WebEntity {ID}: {ContentTypeName}, {Title}")]
	public class WebEntity : Entity, IWebEntity
	{
		/// <summary>
		/// Securable type
		/// </summary>
		public override SecurableType SecurableType
		{
			get { return WebSecurableType.Instance; }
		}

		/// <summary>
		/// Return SharePoint list item
		/// </summary>
		public virtual SPListItem GetItem()
		{
			return (SPListItem)GetSource();
		}

		/// <summary>
		/// SharePoint content type
		/// </summary>
		public SPContentType ContentType
		{
			get
			{
				var item = GetItem();

				return item != null ? item.ContentType : null;
			}
			set
			{
				var item = GetItem();

				if (item != null)
					item[SPBuiltInFieldId.ContentTypeId] = value.Id;
			}
		}

		/// <summary>
		/// SharePoint content type name
		/// </summary>
		public string ContentTypeName
		{
			get
			{
				var item = GetItem();

				return
					item != null && item.ContentType != null
						? item.ContentType.Name
						: null;
			}
			set
			{
				var item = GetItem();

				if (item != null)
					item[SPBuiltInFieldId.ContentType] = value;
			}
		}

		/// <summary>
		/// SharePoint content type id
		/// </summary>
		public SPContentTypeId ContentTypeId
		{
			get
			{
				var item = GetItem();

				return item != null ? item.ContentTypeId : SPContentTypeId.Empty;
			}
			set
			{
				var item = GetItem();

				if (item != null)
					item[SPBuiltInFieldId.ContentTypeId] = value;
			}
		}

		/// <summary>
		/// Return SharePoint file
		/// </summary>
		public virtual SPFile GetFile()
		{
			return GetItem().File;
		}

		/// <summary>
		/// Return SharePoint folder
		/// </summary>
		public virtual SPFolder GetFolder()
		{
			var item = GetItem();

			if (item == null)
				return null;

			var file = item.File;

			return (file != null ? file.ParentFolder : item.Folder);
		}

		/// <summary>
		/// Return folder path from root folder (if empty - no folder)
		/// </summary>
		public virtual string[] GetFolderPath()
		{
			var item = GetItem();
			var folder = GetFolder();

			// No folder
			if (folder == null)
				return new string[0];

			var folderPath = new List<string>();

			// Folder path
			var rootUrl = item.ParentList.RootFolder.Url;

			while (folder != null && !folder.Url.EqIC(rootUrl))
			{
				folderPath.Add(folder.Name);

				folder = folder.ParentFolder;
			}

			return Enumerable.Reverse(folderPath).ToArray();
		}

		//public virtual IEnumerable<SPFolder> GetSPFolderPath()
		//{
		//    var item = GetItem();
		//    var folder = item.File.ParentFolder;

		//    // No folder
		//    if (folder == null)
		//        return new List<SPFolder>();

		//    var folderPath = new List<SPFolder>();

		//    var rootUrl = item.ParentList.RootFolder.Url;

		//    while (folder != null && !folder.Url.EqIC(rootUrl))
		//    {
		//        folderPath.Add(folder);

		//        folder = folder.ParentFolder;
		//    }

		//    return Enumerable.Reverse(folderPath);
		//}
	}
}